import { AxiosResponse } from 'axios'
import { put, takeEvery } from 'redux-saga/effects'
import { getuseInfoSuccess } from '../actions'
import { USERINFO } from '../constants'
import { getUserDetailById, getUserInfo } from '../services/login'

interface IAllApi<T> {
  code: number
  data: T
  message: string
  success: boolean
}

function* profileInfo() {
  const response: AxiosResponse<IAllApi<any>> = yield getUserInfo()

  const res: AxiosResponse<IAllApi<any>> = yield getUserDetailById(response.data.data.userId)

  const userInfo = { ...response.data.data, ...res.data.data }
  localStorage.setItem('erabbit_userInfo', JSON.stringify(userInfo))

  yield put(getuseInfoSuccess(userInfo))
}

export function* profileTake() {
  yield takeEvery(USERINFO, profileInfo)
}
